#!/bin/bash

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # 没有颜色

# 获取IP地址和主机名
IP_ADDR=$(hostname -I | cut -d' ' -f1)
HOSTNAME=$(hostname)

# CPU负载信息：
cpu_load() {
    echo -e "${CYAN}\t\t\tCPU的负载情况${NC}"
    echo -e "\t------------------------------------------------"
    echo -e "\tCPU load in 1  min is: ${YELLOW}`awk  '{printf "%15s",$1}' /proc/loadavg`${NC}"
    echo -e "\tCPU load in 5  min is: ${YELLOW}`awk  '{printf "%15s",$2}' /proc/loadavg`${NC}"
    echo -e "\tCPU load in 10 min is: ${YELLOW}`awk  '{printf "%15s",$3}' /proc/loadavg`${NC}"
    echo
}

# 内存基本信息：
memory_info() {
    echo -e "${CYAN}\t\t\t内存的使用情况${NC}"
    echo -e "\t------------------------------------------------"
    echo -e "\t${GREEN}`free -h | awk '/Mem/{printf "%-10s %s","内存总容量:",$2}'`${NC}"
    echo -e "\t${GREEN}`free -h | awk '/Mem/{printf "%-10s %s","内存空闲容量:",$4}'`${NC}"
    echo -e "\t${GREEN}`free -h | awk '/Mem/{printf "%-10s %s","内存缓存:",$6}'`${NC}"
    echo
}

# 磁盘使用量排序：
disk_rank() {
    echo -e "${CYAN}\t\t\t各分区使用率${NC}"
    echo -e "\t------------------------------------------------"
    df -h  -x tmpfs -x devtmpfs | sort -nr -k 5 | awk '/dev/{printf "\t%-20s %10s\n", $1, $5}'
    echo
}

# 显示系统信息
echo
echo -e "${CYAN}\t\t\t系统基本信息：${NC}"
echo -e "\t------------------------------------------------"
echo -e "\tCurrent Time : ${GREEN}$(date)${NC}"
echo -e "\tVersion      : ${GREEN}$(cat /etc/os-release | grep -w "PRETTY_NAME" | cut -d= -f2 | tr -d '"')${NC}"
echo -e "\tKernel       : ${GREEN}$(uname -r)${NC}"
echo -e "\tUptime       : ${GREEN}$(uptime -p)${NC}"
echo -e "\tIP addr      : ${GREEN}$IP_ADDR${NC}"
echo -e "\tHostname     : ${GREEN}$HOSTNAME${NC}"
echo -e "\tCpu          : ${GREEN}$(lscpu | grep "Model name:" | sed 's/Model name:\s*//')${NC}"
echo -e "\tMemory       : ${GREEN}$(free -h | awk '/^Mem:/ { print $3 "/" $2 }')${NC}"
echo -e "\tSWAP         : ${GREEN}$(free -h | awk '/^Swap:/ { print $3 "/" $2 }')${NC}"
echo -e "\tUsers Logged : ${GREEN}$(who | wc -l) users${NC}"
echo


#将脚本放置在用户主目录下，例如~/system_info.sh，然后修改.bashrc文件，使其在每次登录时自动执行该脚本：
# echo "~/system_info.sh" >> ~/.bashrc
cpu_load
memory_info
disk_rank